<?php

/*
 * 
 * 截取字符串 可支持截取中文字符
 */
if (!function_exists('my_substr')) {

    function my_substr($str, $len, $suffix = '...') {
        // Strip tags
        $str = trim(strip_tags($str));

        $i = 0;
        $l = 0;
        $c = 0;
        $a = array();
        while ($l <= $len) {
            $t = substr($str, $i, 1);
            if (ord($t) >= 224) {
                $c = 3;
                $t = substr($str, $i, $c);
                $l += 2;
            } elseif (ord($t) >= 192) {
                $c = 2;
                $t = substr($str, $i, $c);
                $l += 2;
            } else {
                $c = 1;
                $l++;
            }
            $i += $c;
            $a[] = $t;
        }
        $sub = implode('', $a);
        if (strlen($str) > $len)
            $sub .= $suffix;
        return $sub;
    }

}

if (!function_exists('my_trim_html_tags')) {

    function my_trim_html_tags($str) {
        return preg_replace("/<[^<]*>|(?:&nbsp;){2,}/", '', $str);
    }

}

if (!function_exists('plain_text')) {

    function plain_text($str) {
        return preg_replace("/<[^<]*>|(?:&nbsp;)+/", '', $str);
    }

}

if (!function_exists('utf8_strlen')) {

    // 计算中文字符串长度
    function utf8_strlen($string = null) {
        // 将字符串分解为单元
        preg_match_all("/./us", $string, $match);
        // 返回单元个数
        return count($match[0]);
    }

}
